package com.study.guava;

import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Table;

import java.util.Collection;
import java.util.Map;
import java.util.Set;

/**
 * @author: jzhou
 * @date: 2024/12/1-11:21
 * @version: 1.0
 * @description: juc-study
 */
public class GuavaTableAdvancedExample {
    public static void main(String[] args) {
        // 创建一个Table实例
        Table<String, String, Integer> workHoursTable = HashBasedTable.create();

        // 添加数据
        workHoursTable.put("Alice", "ProjectA", 40);
        workHoursTable.put("Bob", "ProjectA", 30);
        workHoursTable.put("Alice", "ProjectB", 20);
        workHoursTable.put("Charlie", "ProjectC", 50);

        // 检索数据
        Integer aliceProjectAHours = workHoursTable.get("Alice", "ProjectA");
        System.out.println("使用 get() 检索数据：Alice worked " + aliceProjectAHours + " hours on ProjectA.");

        // 使用row()方法获取特定行的映射
        Map<String, Integer> aliceWorkHours = workHoursTable.row("Alice");
        System.out.println("使用 row() 方法获取特定行的映射：Alice's work hours " + aliceWorkHours);

        // 使用column()方法获取特定列的映射
        Map<String, Integer> projectAWorkHours = workHoursTable.column("ProjectA");
        System.out.println("使用 column() 方法获取特定列的映射： Work hours on ProjectA " + projectAWorkHours);

        // 遍历表格
        System.out.println("遍历 ------cellSet()--------> 开始");
        for (Table.Cell<String, String, Integer> cell : workHoursTable.cellSet()) {
            System.out.println(cell.getRowKey() + " worked " + cell.getValue() + " hours on " + cell.getColumnKey() + ".");
        }
        System.out.println("遍历 <------cellSet()-------- 解释");

        // 修改数据
        workHoursTable.put("Alice", "ProjectA", aliceProjectAHours + 5);
        // Alice在ProjectA上多工作了5小时
        System.out.println("使用 put() 修改数据后 " + workHoursTable.get("Alice", "ProjectA") + " hours on ProjectA.");

        // 检查是否包含某个键值对
        boolean hasBobOnProjectB = workHoursTable.contains("Bob", "ProjectB");
        System.out.println("判断包含 contains()：Does Bob work on ProjectB? " + hasBobOnProjectB);

        // 检查行键或列键是否存在
        boolean hasRowKeyCharlie = workHoursTable.containsRow("Charlie");
        boolean hasColumnKeyProjectD = workHoursTable.containsColumn("ProjectD");
        System.out.println("判断行包含 containsRow()：Does the table have a row for Charlie? " + hasRowKeyCharlie);
        System.out.println("判断列包含 containsColumn()：Does the table have a column for ProjectD? " + hasColumnKeyProjectD);

        // 获取所有的行键、列键或值
        Set<String> allRowKeys = workHoursTable.rowKeySet();
        Set<String> allColumnKeys = workHoursTable.columnKeySet();
        Collection<Integer> allValues = workHoursTable.values();

        System.out.println("获取所有的行键 rowKeySet() All row keys: " + allRowKeys);
        System.out.println("获取所有的列键 columnKeySet() column keys: " + allColumnKeys);
        System.out.println("获取所有的值 values() All values: " + allValues);

        // 移除数据
        workHoursTable.remove("Alice", "ProjectB");
        // Alice不再在ProjectB上工作
        System.out.println("删除数据：remove()  After removal, Alice's work hours on ProjectB: " + workHoursTable.get("Alice", "ProjectB"));
    }
}
